package org.springsecuritylearn.admin.control;

import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springsecuritylearn.admin.dto.BookCondition;
import org.springsecuritylearn.admin.dto.BookInfo;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hyf
 * @Title: ${file_name}
 * @Package org.springsecuritylearn.admin.control
 * @Description: ${todo}
 * @date 2018/5/2下午10:30
 */

@RestController
@RequestMapping("/book/")
public class BookController {

    @RequestMapping(value = "book_query",method = RequestMethod.GET)
    public List<BookInfo> query(@RequestParam(value = "name",defaultValue = "SB") String name){
        System.out.println(name);
        List<BookInfo> bookInfoList=new ArrayList<BookInfo>();
        bookInfoList.add(new BookInfo(1L,"cctv"));
        bookInfoList.add(new BookInfo(2L,"ccav"));
        bookInfoList.add(new BookInfo(3L,"战争与核平"));
        return bookInfoList;
    }

    @RequestMapping(value = "book_query2",method = RequestMethod.GET)
    public List<BookInfo> query2(@RequestParam(value = "name",defaultValue = "SB") String name, BookCondition bookCondition){
        System.out.println(name);
        System.out.println(bookCondition.getName());
        System.out.println(bookCondition.getPrice());
        List<BookInfo> bookInfoList=new ArrayList<BookInfo>();
        bookInfoList.add(new BookInfo(1L,"cctv"));
        bookInfoList.add(new BookInfo(2L,"ccav"));
        bookInfoList.add(new BookInfo(3L,"战争与核平"));
        return bookInfoList;
    }


    @RequestMapping(value = "book_query3/{id}",method = RequestMethod.GET)
    public BookInfo bookQuery(@PathVariable  Long id){
        BookInfo bookInfo=new BookInfo(1L,"cctv");
        return bookInfo;
    }

    @GetMapping("book_query4/{id:\\d}")
    public BookInfo bookQuery2(@PathVariable  Long id){
        BookInfo bookInfo=new BookInfo(1L,"cctv");
        return bookInfo;
    }


    @PostMapping(value = "blantest1")
    public BookInfo  blankTest(@Valid  BookInfo bookInfo, BindingResult bindingResult){//,int id,String name
        if (bindingResult.hasErrors()){
            bindingResult.getAllErrors().forEach(e-> System.out.println(e));
        }

        System.out.println(bookInfo);
        bookInfo.setId(99L);
        bookInfo.setName("gg");
        return bookInfo;
    }


    @PostMapping(value = "blantest2")
    public BookInfo  blankTest2(@Valid  BookInfo bookInfo, BindingResult bindingResult,@CookieValue  String ff){//,int id,String name
        if (bindingResult.hasErrors()){
            bindingResult.getAllErrors().forEach(e-> System.out.println(e));
        }
        System.out.println(ff);
        System.out.println(bookInfo);
        bookInfo.setId(99L);
        bookInfo.setName("gg");
        return bookInfo;
    }


    @GetMapping(value = "errorTest")
    public String   errorTest()  {//,int id,String name
        throw  new RuntimeException("这是一个运行时错误");
//        return "success";
    }
}
